/**
 * @file    app_pow_mng.c
 * @brief   用户任务-电源管理任务
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-05-03
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-06-07 <td>1.0.1     <td>Liuwei   <td>首次创建
 * <tr><td>2023-06-07 <td>1.0.1     POW_OFF_DLY 修改为1000 下电体验好一点
 * <tr><td>2024-10-21 <td>1.0.2     <td>Liuwei   <td>修改低压休眠逻辑策略，欠压保护延时30S，低压休眠休眠1H
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "app_diagnosis.h"
#include "FreeRTOS.h"
#include "task.h"
#include "If_can.h"
#include "diagnosis.h"
#include "diag_curr.h"
#include "diag_sys.h"
#include "diag_volt.h"
#include "diag_temp.h"
#include "var_mng.h"
#include "param_mng.h"
#include "dev_afe.h"
#include "If_do.h"
#include "param_mng.h"
#include "var_mng.h"
#include "If_di.h"
#include "app_disp.h"
#include "parallel_com.h"
#include "app_monitor.h"
#include "If_wdg.h"
#include "boot_param.h"
#include "If_com.h"
#include "drv_lpm.h"
#include "drv_gpio.h"
#include "parallel.h"
#include "sample.h"
#include "param_mng.h"
#include "diagnosis.h"
#include "drv_clk.h"
#include "drv_adc.h"
#include "cfg_prj.h"
#include "drv_spi.h"
#include "event_mng.h"
#include "base_math.h"
/* macro/define --------------------------------------------------------------*/
#define APP_POW_TASK_CYCLE                   (50) 
#define LP_SLEEP_DELAY                       (120000)    // 电压欠压休眠  (1H)
#define KEY_DOWN_SLEEP                       (3000)
#define KEY_DOWN_RESET                       (6000)
#define FAULT_SLEEP_DELAY                    (1800000)   
#define CUV_SLEEP_DELAY						 (60000)      // 欠压保护后60S进入休眠
#define POW_OFF_DLY                          (1000)  
#define SLEEP_SRC_KEY                        (0x55A0)
#define SLEEP_SRC_LP                         (0x55A1)
#define SLEEP_SRC_SW                         (0x55A2)         

#define SYS_LP_STOP       (0)
#define SYS_LP_STADNBY    (1)
#define SYS_LP_SHUNTDOWN  (2)

/* typedef -------------------------------------------------------------------*/
typedef struct 
{
    uint8_t state ; /* !<初始状态 */
    GPIO_PORT port;
    GPIO_PIN pin;
}do_map_lp_type;
/* local functions -----------------------------------------------------------*/
static void system_reset_before_deal(void);
static void sleep_before_php_deal(void);
static void sleep_before_io_deal(void);
static void system_into_sleep(uint8_t sleep_state);
static void sleep_wakeup_after_deal(void);
void system_stanby_task(void);
static void sleep_stop_io_deal(void);
/* static variables ----------------------------------------------------------*/
static uint32_t col_sleep_cnt = 0;
static uint32_t pow_sign_off_cnt = 0;
static uint32_t key_down_cnt = 0;
static uint32_t idie_sleep_cnt = 0; // 待机休眠
static uint32_t cuv_sleep_cnt = 0;  // 欠压休眠
static uint32_t lp_sleep_dly_cnt = 0; // 低压休眠
static uint32_t idle_delay = 0;
/* global variables --------------------param_set_data(--------------------------------------*/
extern TaskHandle_t DispLEDTask_Handler;
extern TaskHandle_t NvmTask_Handler;
extern TaskHandle_t SampleTaskHandler;
extern TaskHandle_t ComCommTaskHandler;
extern TaskHandle_t CanCommTaskHandler;
extern TaskHandle_t MonitorTaskHandler;
extern TaskHandle_t SoxTaskHandler;
extern TaskHandle_t DiagTaskHandler;
/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief 诊断任务
*/
void vPowMngTask(void *pvParameters)
{
	uint16_t sleep_flag = 0;
	uint32_t task_cycle = 0;
	float smp_curr = 0;
	uint8_t sys_state = 0;
	uint16_t min_volt = 0;
    portTickType xLastExecutionTime;
    xLastExecutionTime = xTaskGetTickCount();
	
	task_run_reset(TASK_ID_POW_MNG);
	
    while(1)
    {
		sys_state = var_get_data(VAR_ID_CURR_DR);
		smp_curr = var_get_data(VAR_ID_BUS_CURR) * VAR_CURR_FACTOR - VAR_CURR_OFFSET;
		task_cycle = task_get_cycle(TASK_ID_POW_MNG);
		if(task_cycle >= APP_POW_TASK_CYCLE * 8)
			task_cycle = APP_POW_TASK_CYCLE;
		/* 单体掉线和AFE掉线故障无法判断单体是否正常，所以采用故障休眠延时 */
        if(diag_get_prp(PRP_ID_COL) || diag_get_prp(PRP_ID_FT_AFE))
        {
            col_sleep_cnt += task_cycle;
            if(col_sleep_cnt >= FAULT_SLEEP_DELAY)
            {
                sleep_flag = SLEEP_SRC_LP;
            }
        }
        else
        {
            col_sleep_cnt = 0;
			/* 欠压保护后，进入休眠逻辑 */
			if(PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))
			{
				// 主机状态下 无从机 60S进入休眠
				if(master_data_reg.parallel_num == 1)
				{
					if(((TRUE == diag_get_prp(PRP_ID_CUV)) || (TRUE == diag_get_prp(PRP_ID_BUV))) && (CURR_DR_CHG != sys_state))
					{	
						// 欠压保护后 持续无充电，立即进入休眠
						cuv_sleep_cnt += task_cycle;
						if(cuv_sleep_cnt >= CUV_SLEEP_DELAY)
						{
							event_add_to_sram(EVT_ID_LPW_SLEEP , var_get_data(VAR_ID_MIN_CELL_VOLT), param_get_data(PAR_ID_IDE_SLP_VOLT));	
							vTaskDelay(100);
							sleep_flag = SLEEP_SRC_LP;
						}
					}
					else
					{
						cuv_sleep_cnt = 0;
					}
				}
				
				min_volt = var_get_data(VAR_ID_MIN_CELL_VOLT);
				// 最低单体电压低于保护电压-差值，持续60S进入休眠 */
				if((min_volt <= param_get_data(PAR_ID_CUV_PRP_VAL) - LP_SLEEP_VDIFF) && (CURR_DR_CHG != sys_state))
				{
					lp_sleep_dly_cnt += task_cycle;
					if(lp_sleep_dly_cnt >= LP_SLEEP_DELAY)
					{
						sleep_flag = SLEEP_SRC_LP;
					}
				}
				else
				{
					lp_sleep_dly_cnt = 0;
				}
			}
			else
			{
				// 非主机 持续60S进入休眠
				if(((TRUE == diag_get_prp(PRP_ID_CUV)) || (TRUE == diag_get_prp(PRP_ID_BUV)))&& (CURR_DR_CHG != sys_state))
				{	
					// 欠压保护后 持续无充电，立即进入休眠
					cuv_sleep_cnt += task_cycle;
					if(cuv_sleep_cnt >= CUV_SLEEP_DELAY)
					{
						event_add_to_sram(EVT_ID_LPW_SLEEP , var_get_data(VAR_ID_MIN_CELL_VOLT), param_get_data(PAR_ID_IDE_SLP_VOLT));	
						vTaskDelay(100);					
						sleep_flag = SLEEP_SRC_LP;
					}
				}
				else
				{
					cuv_sleep_cnt = 0;
				}
			}
        }
		/* 检测按键休眠 */
		if(If_di_read_in(DI_RST_SIGN) == DI_IN_ON)
		{
			key_down_cnt += task_cycle;
			if(key_down_cnt >= KEY_DOWN_RESET)
			{
				if(led_disp_get_state() == LED_SLEEP_CMP)
				{
					led_disp_set_state(LED_RUN_STATE);
					key_down_cnt = KEY_DOWN_RESET;
					system_reset_before_deal();
				}
			}
			else if(key_down_cnt >= KEY_DOWN_SLEEP)
			{
				led_disp_set_state(LED_SLEEP_STATE);
			}
			else
			{
				;
			}
		}
		else
		{
			if((key_down_cnt >= KEY_DOWN_SLEEP) && (key_down_cnt < KEY_DOWN_RESET))
			{
				if(led_disp_get_state() == LED_SLEEP_CMP)
				{
					sleep_flag = SLEEP_SRC_KEY;
					key_down_cnt = 0; 
				}
			}
			else
			{
				key_down_cnt = 0;
			}
		}
		

		if(smp_curr < 0)
		{
			smp_curr = -smp_curr;
		}
		// 深度待机休眠检测：无通讯，电流小于休眠电流，电压低于某个值时，持续24H后，进入深度休眠。
		if((var_get_data(VAR_ID_COMM_STATE) != 0)
			&& (smp_curr < param_get_data(PAR_ID_IDE_SLP_CURR) * 0.1)
			&& (var_get_data(VAR_ID_MIN_CELL_VOLT) < param_get_data(PAR_ID_IDE_SLP_VOLT)))
		{
			idie_sleep_cnt += task_cycle;
			if(idie_sleep_cnt >= param_get_data(PAR_ID_IDE_SLP_DLY) * 60000)
			{
				event_add_to_sram(EVT_ID_LPW_SLEEP , var_get_data(VAR_ID_MIN_CELL_VOLT), param_get_data(PAR_ID_IDE_SLP_VOLT));	
                vTaskDelay(100);
				sleep_flag = SLEEP_SRC_LP;
			}
		}
		else
		{
			idie_sleep_cnt = 0;
		}
		//设置一个最小值不小于2H
		// 深度待机休眠检测：无通讯，电流小于休眠电流，电压低于某个值时，持续24H后，进入待机休眠。
		if ((var_get_data(VAR_ID_COMM_STATE) != 0)
			&& (CALC_ABS(smp_curr) < ZERO_DRIFT))
		{
			idle_delay += task_cycle;
			if(idle_delay >= param_get_data(PAR_ID_IDE_SLEEP_DELAY)* 60000)
			{
				event_add_to_sram(EVT_ID_CMD_SLEEP , var_get_data(VAR_ID_BUS_CURR), param_get_data(PAR_ID_IDE_SLP_VOLT));	
				vTaskDelay(100);	
				idle_delay = param_get_data(PAR_ID_IDE_SLEEP_DELAY)* 60000;	
				
				var_set_data(VAR_ID_CTRL_SLEEP , CMD_STANDBY_SLEEP);
			}
		}
		else
		{
			idle_delay = 0;
		}		
		
		// 复位系统
		if(USE_CMD_RESET == var_get_data(VAR_ID_USE_CMD))
        {
            system_reset_before_deal();
        }
        else
        {
            ;
        }
#if (USE_SW_POW == 1)
		if(If_di_read_in(DI_SW_SIGN) == DI_IN_OFF)
		{
			pow_sign_off_cnt += task_cycle;
			if(pow_sign_off_cnt >= POW_OFF_DLY)
			{
				pow_sign_off_cnt = 0;
				sleep_flag = SLEEP_SRC_SW;  // 由KEY修改为SW
			}
		}
		else
		{
			pow_sign_off_cnt = 0;
		}
#endif
		/* 系统进入休眠 */
		if((SLEEP_SRC_KEY == sleep_flag)
            ||(SLEEP_SRC_LP == sleep_flag) 
		    ||(SLEEP_SRC_SW == sleep_flag)
            ||(var_get_data(VAR_ID_USE_CMD) == 0x55AA)
            ||(var_get_data(VAR_ID_CTRL_SLEEP) == CMD_DEEP_SLEEP)
			||(var_get_data(VAR_ID_CTRL_SLEEP) == CMD_STANDBY_SLEEP)
			||(var_get_data(VAR_ID_CTRL_SLEEP) == CMD_LOW_SLEEP)
			||(var_get_data(VAR_ID_REMOTE_CMD) == REMOTE_CMD_SLEEP))
		{
            vTaskSuspend(SampleTaskHandler);
            vTaskDelay(100); 
            dev_afe_ctrl_mos(0 , 0);
            vTaskDelay(50); 
            if((If_di_read_in(DI_CHG_SIGN) == DI_IN_ON)&&(If_di_read_in(DI_SW_SIGN) == DI_IN_ON))
            {
				sleep_flag = 0;
				var_set_data(VAR_ID_USE_CMD , 0);
				var_set_data(VAR_ID_CTRL_SLEEP , 0);
                vTaskResume(SampleTaskHandler);
				led_disp_set_state(LED_RUN_STATE);
            }
            else
            {
                /* 进入休眠状态 */
                vTaskSuspend(NvmTask_Handler);
                vTaskSuspend(SampleTaskHandler);
                vTaskSuspend(ComCommTaskHandler);
                vTaskDelay(50); 
                If_wdg_feed();
				task_run_count(TASK_ID_POW_MNG);
			    param_save_use_param();
               	dev_afe_into_sleep(); //20241029
                If_do_set_output(DO_DMOS_CTRL , DO_OUT_OFF);
                vTaskDelay(100);
                If_wdg_feed();
                /** 如果是主机 且按键进入休眠 则下发广播进入休眠 */
                if((If_di_read_in(DI_SW_SIGN) == DI_IN_OFF) && (PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR)))
                {
                    parallel_com_master_broadcast_sleep(COM_PARALLEL);
                    vTaskDelay(100);
                    parallel_com_master_broadcast_sleep(COM_PARALLEL);
                    vTaskDelay(100);
                    parallel_com_master_broadcast_sleep(COM_PARALLEL);
                }
				else
				{
					If_wdg_feed();
					vTaskDelay(400);
					task_run_count(TASK_ID_POW_MNG);
				}
				If_do_set_output(DO_SYS_DCDC , DO_OUT_OFF);
                vTaskDelay(100);
#if (USE_SW_POW == 1)
				If_do_set_output(DO_SYS_POW , DO_OUT_OFF);
                vTaskDelay(100); 
				
				if((sleep_flag == SLEEP_SRC_LP)||(var_get_data(VAR_ID_CTRL_SLEEP) == CMD_DEEP_SLEEP)||(SLEEP_SRC_SW == sleep_flag)||(sleep_flag == SLEEP_SRC_KEY))
					system_into_sleep(SYS_LP_SHUNTDOWN);
				else
					system_into_sleep(SYS_LP_STADNBY);
#else 
				if((sleep_flag == SLEEP_SRC_LP)||(var_get_data(VAR_ID_CTRL_SLEEP) == CMD_DEEP_SLEEP))
					system_into_sleep(SYS_LP_SHUNTDOWN);
				else
					system_into_sleep(SYS_LP_STADNBY);
				
                If_do_set_output(DO_SYS_POW , DO_OUT_OFF);
                vTaskDelay(20); 
#endif
            }
		}
        
        task_run_count(TASK_ID_POW_MNG);
        
        vTaskDelayUntil( &xLastExecutionTime, APP_POW_TASK_CYCLE);
    }
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
static void system_reset_before_deal(void)
{
	/* 进入休眠状态 */
	vTaskSuspend(DispLEDTask_Handler);
	vTaskSuspend(NvmTask_Handler);
	vTaskSuspend(SampleTaskHandler);
	vTaskDelay(100);
	// dev_afe_ctrl_mos(0 , 0);
	vTaskDelay(10);
	param_save_use_param();
    NVIC_SystemReset();
}

static void system_into_sleep(uint8_t sleep_state)
{
	switch(sleep_state)
	{
		case SYS_WAIT_SLEEP:
			sleep_stop_io_deal();
		    // 复位按键激活
			GPIO_CLR_INT_FLAG(PC, BIT0);
			GPIO_SetMode(PC, BIT0, GPIO_MODE_INPUT);
			GPIO_EnableInt(PC, 0, GPIO_INT_FALLING);
			NVIC_EnableIRQ(GPC_IRQn);
			GPIO_SetPullCtl(PC , BIT0 , GPIO_PUSEL_PULL_UP);
			// 
			GPIO_CLR_INT_FLAG(PB, BIT2);
			GPIO_SetMode(PB, BIT2, GPIO_MODE_INPUT);
			GPIO_EnableInt(PB, 2, GPIO_INT_FALLING);
			NVIC_EnableIRQ(GPB_IRQn);
			
			// 自锁按键唤醒
			GPIO_CLR_INT_FLAG(PH, BIT10);
			GPIO_SetMode(PH, BIT10, GPIO_MODE_INPUT);
			GPIO_EnableInt(PH, 10, GPIO_INT_BOTH_EDGE);
			NVIC_EnableIRQ(GPH_IRQn);
			// 充电激活
			GPIO_CLR_INT_FLAG(PB, BIT0);
			GPIO_SetMode(PB, BIT0, GPIO_MODE_INPUT);
			GPIO_EnableInt(PB, 0, GPIO_INT_BOTH_EDGE);
			NVIC_EnableIRQ(GPB_IRQn);
			GPIO_SetPullCtl(PB , BIT0 , GPIO_PUSEL_PULL_UP);
		
			drv_lmp_into_idle_sleep();
			break;
		case SYS_LP_STOP :			
			break;
		case SYS_LP_STADNBY : 
			sleep_before_php_deal();
			sleep_before_io_deal();
			drv_lpm_into_mode(LMP_MODE_SHUT_DOWN , LMP_WK_KEY_PIN | LMP_WK_CHG_PIN |LMP_WK_COM_PIN | LMP_SW_KEY_PIN   , 0);
			break;
		case SYS_LP_SHUNTDOWN :
			sleep_before_php_deal();
			sleep_before_io_deal();
			drv_lpm_into_mode(LMP_MODE_SHUT_DOWN , LMP_WK_KEY_PIN | LMP_WK_CHG_PIN | LMP_SW_KEY_PIN, 0);
			break;
		default:
			drv_lpm_into_mode(LMP_MODE_SHUT_DOWN , LMP_WK_CHG_PIN | LMP_WK_KEY_PIN |LMP_WK_COM_PIN | LMP_WK_CAN_PIN , 0);
			break;
	}
}

static void sleep_wakeup_after_deal(void)
{
//	GPIO_CLR_INT_FLAG(PC, BIT0);
//	GPIO_DisableInt(PC, 0);	
//	GPIO_CLR_INT_FLAG(PB, BIT0);
//	GPIO_DisableInt(PB, 0);
//	GPIO_CLR_INT_FLAG(PH, BIT10);
//	GPIO_DisableInt(PH, 6);
//	
	drv_clk_set_sys(SYS_FREQ_MID);
	
	SYS->GPA_MFPH = (SYS_GPA_MFPH_PA8MFP_UART1_RXD | SYS_GPA_MFPH_PA9MFP_UART1_TXD) | SYS_GPA_MFPH_PA15MFP_SPI2_MOSI | SYS_GPA_MFPH_PA14MFP_SPI2_MISO | SYS_GPA_MFPH_PA13MFP_SPI2_CLK | SYS_GPA_MFPH_PA12MFP_SPI2_SS;
    SYS->GPA_MFPL = (SYS_GPA_MFPL_PA0MFP_UART0_RXD | SYS_GPA_MFPL_PA1MFP_UART0_TXD)|(SYS_GPA_MFPL_PA4MFP_UART5_RXD | SYS_GPA_MFPL_PA5MFP_UART5_TXD);
    SYS->GPB_MFPH = (SYS_GPB_MFPH_PB8MFP_EADC0_CH8   | SYS_GPB_MFPH_PB9MFP_EADC0_CH9   |
                      SYS_GPB_MFPH_PB10MFP_EADC0_CH10 | SYS_GPB_MFPH_PB11MFP_EADC0_CH11 |
                      SYS_GPB_MFPH_PB12MFP_EADC0_CH12 | SYS_GPB_MFPH_PB13MFP_EADC0_CH13 |
                      SYS_GPB_MFPH_PB14MFP_EADC0_CH14 | SYS_GPB_MFPH_PB15MFP_EADC0_CH15 );
	SYS->GPB_MFPL = (SYS_GPB_MFPL_PB0MFP_UART2_RXD | SYS_GPB_MFPL_PB1MFP_UART2_TXD)|SYS_GPB_MFPL_PB6MFP_EADC0_CH6 | SYS_GPB_MFPL_PB7MFP_EADC0_CH7;
    SYS->GPC_MFPH = (SYS_GPC_MFPH_PC11MFP_UART6_RXD | SYS_GPC_MFPH_PC12MFP_UART6_TXD);
    SYS->GPE_MFPH = (SYS_GPE_MFPH_PE10MFP_UART3_TXD | SYS_GPE_MFPH_PE11MFP_UART3_RXD);
	
	If_di_init_all();
	
	drv_gpio_cfg_mode(SYS_POW_PORT  , SYS_POW_PIN  , GPIO_MODE_OUTPUT);
    drv_gpio_set_pin(SYS_POW_PORT , SYS_POW_PIN);
}

static void sleep_before_php_deal(void)
{
	CLK->APBCLK1 = 0x00000000;
	CLK->APBCLK0 = 0x00000000;
}

static void sleep_before_io_deal(void)
{
	SYS->GPA_MFPH = 0;
    SYS->GPA_MFPL = 0;
    SYS->GPB_MFPH = 0;
    SYS->GPB_MFPL = 0;
    SYS->GPC_MFPH = 0;
    SYS->GPC_MFPL = 0;
    SYS->GPD_MFPH = 0;
    SYS->GPD_MFPL = 0;
    SYS->GPE_MFPH = 0;
    SYS->GPE_MFPL = 0;
    SYS->GPF_MFPH = 0;
    SYS->GPF_MFPL = 0x000000EE; //ICE pin
    SYS->GPG_MFPH = 0;
    SYS->GPG_MFPL = 0;
    SYS->GPH_MFPH = 0;
    SYS->GPH_MFPL = 0;

	 /* Set all GPIOs are output mode */
    PA->MODE = 0x55555555;
    PB->MODE = 0x55555505;
    PC->MODE = 0x51555554;
    PD->MODE = 0x54545555;
    PE->MODE = 0x55555555;
    PF->MODE = 0x55550555;
    PG->MODE = 0x55555555;
    PH->MODE = 0x55555555;

    /* Set all GPIOs are output high */
#if (USE_SW_POW == 1)
    PA->DOUT = 0x0000;
#else 
	PA->DOUT = 0x0080;
#endif
    PB->DOUT = 0x0001;
    PC->DOUT = 0xC1C0;
    PD->DOUT = 0x80FF;
    PE->DOUT = 0xF103;
    PF->DOUT = 0xFF03;
    PG->DOUT = 0x001C;
    PH->DOUT = 0xF03F;
}

static void sleep_stop_io_deal(void)
{
	SYS->GPA_MFPH = 0;
    SYS->GPA_MFPL = 0;
    SYS->GPB_MFPH = 0;
    SYS->GPB_MFPL = 0;
    SYS->GPC_MFPH = 0;
    SYS->GPC_MFPL = 0;
    SYS->GPD_MFPH = 0;
    SYS->GPD_MFPL = 0;
    SYS->GPE_MFPH = 0;
    SYS->GPE_MFPL = 0;
    SYS->GPF_MFPH = 0;
    SYS->GPF_MFPL = 0x000000EE; //ICE pin
    SYS->GPG_MFPH = 0;
    SYS->GPG_MFPL = 0;
    SYS->GPH_MFPH = 0;
    SYS->GPH_MFPL = 0;

	 /* Set all GPIOs are output mode */
    PA->MODE = 0x55555555;
    PB->MODE = 0x55555504;
    PC->MODE = 0x51555554;
    PD->MODE = 0x54545555;
    PE->MODE = 0x55555555;
    PF->MODE = 0x55554555;
    PG->MODE = 0x55555555;
    PH->MODE = 0x55455555;

	GPIO_SetPullCtl(PB , BIT2 , GPIO_PUSEL_PULL_UP);
	GPIO_SetPullCtl(PH , BIT10 , GPIO_PUSEL_PULL_UP);
    /* Set all GPIOs are output high */
    PA->DOUT = 0x0080;
    PB->DOUT = 0x0000;
    PC->DOUT = 0xC1C0;
    PD->DOUT = 0x80FF;
    PE->DOUT = 0xF103;
    PF->DOUT = 0xFF43;
    PG->DOUT = 0x401C;
    PH->DOUT = 0xF03F;
}

void system_stanby_task(void)
{
	// drv_can_deinit(CAN_ID0);
	drv_can_deinit(CAN_ID1);
		
	drv_adc_deinit();
			
	vTaskDelay(300);
			
	while(1)
	{
		If_wdg_feed();
			
		sleep_wk_src = 0;	
		
		system_into_sleep(SYS_WAIT_SLEEP);
		
		sleep_wakeup_after_deal();

		If_wdg_feed();
	
		switch(sleep_wk_src)
		{
			case SLEEP_WK_SRC_WDT:
				If_wdg_feed();
				// sample_afe_deal_sleep(5000);
				// 有充电电流则系统重启唤醒系统
				break;
			case SLEEP_WK_SRC_CHG:
				NVIC_SystemReset();
				break;
			case SLEEP_WK_SRC_KEY:
				NVIC_SystemReset();
				break;
			case SLEEP_WK_SRC_COM:
				break;
			case SLEEP_WK_SRC_SW:
				if(If_di_read_in(DI_SW_SIGN) == DI_IN_OFF)
				{
					vTaskDelay(50);
					if(If_di_read_in(DI_SW_SIGN) == DI_IN_OFF)
					{
						dev_afe_into_sleep();
						If_do_set_output(DO_SYS_POW , DO_OUT_OFF);
						vTaskDelay(50);
						system_into_sleep(SYS_LP_SHUNTDOWN);
					}
				}
				else
				{
					
				}
				break;
			default:
				break;
		}
	}
}

void system_deep_sleep_task(void)
{		
	If_do_set_output(DO_SYS_DCDC , DO_OUT_OFF);

    vTaskDelay(100);
	If_do_set_output(DO_SYS_POW , DO_OUT_OFF);

	system_into_sleep(SYS_LP_SHUNTDOWN);
}
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
